//
//  YJResultFrameModel.m
//  练习04
//
//  Created by hcios on 2017/7/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "YJResultFrameModel.h"

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

#define TitleFont [UIFont systemFontOfSize:15]
#define ContextFont [UIFont systemFontOfSize:13]

@implementation YJResultFrameModel

-(void)setModel:(YJResultModel *)model{
    
    _model = model;
    CGFloat padding = 10;
    
    //标题
    if ([model.nodes_entry_name isEqualToString:@""]) {
        self.titleFrame = CGRectZero;
    }else{
        CGSize titleSize = [self sizeWithText:model.nodes_entry_name font:TitleFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        CGFloat titleW = titleSize.width;
        CGFloat titleH = titleSize.height;
        self.titleFrame = CGRectMake(padding, padding, titleW, titleH);
    }
    
    //文字
    if ([model.nodes_comment isEqualToString:@""]) {
        self.topLabFrame = CGRectMake(padding, CGRectGetMaxY(self.titleFrame)+padding, 0, 0);
    }else{
        CGSize topLabSize = [self sizeWithText:model.nodes_comment font:ContextFont maxSize:CGSizeMake(SCREEN_WIDTH - padding * 2, MAXFLOAT)];
        self.topLabFrame = CGRectMake(padding, CGRectGetMaxY(self.titleFrame)+5, topLabSize.width, topLabSize.height);
    }
    
    //图像
    //判断图片是否存在
    if ([model.photo_image_width isEqualToString:@""]) {
        self.imageFrame = CGRectMake(padding, CGRectGetMaxY(self.topLabFrame)+5, 0, 0);
    }else{
        CGFloat iconOW = [model.photo_image_width integerValue];
        CGFloat iconOH = [model.photo_image_height integerValue];
        CGFloat bili = iconOH / iconOW;
        CGFloat iconX = padding;
        CGFloat iconY = CGRectGetMaxY(self.topLabFrame)+5;
        CGFloat iconW = SCREEN_WIDTH - (2*padding);
        CGFloat iconH = bili * iconW;
        self.imageFrame = CGRectMake(iconX, iconY, iconW, iconH);
    }
    
    //文字
    if ([model.notes_description isEqualToString:@""]) {
        self.contextFrame = CGRectMake(padding, CGRectGetMaxY(self.imageFrame)+5, 0, 0);
    }else{
        CGSize textSize = [self sizeWithText:model.notes_description font:ContextFont maxSize:CGSizeMake(SCREEN_WIDTH - padding * 2, MAXFLOAT)];
        self.contextFrame = CGRectMake(padding, CGRectGetMaxY(self.imageFrame)+5, textSize.width, textSize.height);
    }
    
    _cellHeight = CGRectGetMaxY(self.contextFrame) + padding * 2;
    
}

//计算字的大小
-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}


@end
